package com.yuke.cloud.service.uac.service.impl;

import com.google.common.base.Preconditions;
import com.yuke.cloud.common.core.support.BaseService;
import com.yuke.cloud.service.api.smc.dto.ShopInfoDto;
import com.yuke.cloud.service.uac.dao.AccDeliveryAddressMapper;
import com.yuke.cloud.service.uac.entity.AccDeliveryAddress;
import com.yuke.cloud.service.uac.service.AccDeliveryAddressService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * Created by wg on 2018/11/17.
 */
@Service
@Transactional
public class AccDeliveryAddressServiceImpl extends BaseService<AccDeliveryAddress> implements AccDeliveryAddressService {
    @Resource
    private AccDeliveryAddressMapper accDeliveryAddressMapper;

    @Override
    public AccDeliveryAddress getDefaultDeliveryAddressByUserId(Long userId) {
        Preconditions.checkArgument(userId != null, "用户id为空");

        Example example = new Example(AccDeliveryAddress.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId", userId).andEqualTo("isDefault", true);
        List<AccDeliveryAddress> list = accDeliveryAddressMapper.selectByExample(example);
        AccDeliveryAddress accDeliveryAddress = null;
        if (list.size() > 0) {
            accDeliveryAddress = list.get(0);
        }
         return accDeliveryAddress;
    }

    @Override
    public List<AccDeliveryAddress> selectDeliverAddressInfoByUserId(Long userId) {
        Preconditions.checkArgument(userId != null, "用户id为空");

        return accDeliveryAddressMapper.selectDeliverAddressInfoByUserId(userId);
    }

    @Override
    public AccDeliveryAddress selectDefaultDeliveryAddressByUserId(Long userId) {
        Preconditions.checkArgument(userId != null, "用户id为空");

        List<AccDeliveryAddress> list = accDeliveryAddressMapper.selectDefaultDeliveryAddressByUserId(userId);
        if (list != null && list.size() > 0) {
            return list.get(0);
        }

        // 当删除掉默认地址后，无法取得默认地址时，从已有的地址中选择第一个作为默认地址
        if (list == null || list.size() <= 0) {
            List<AccDeliveryAddress> listTemp = this.selectDeliverAddressInfoByUserId(userId);
            if (listTemp != null && listTemp.size() > 0) {
                return listTemp.get(0);
            }
        }

        return null;
    }

    @Override
    public void saveDeliveryAddress(AccDeliveryAddress accDeliveryAddress) {

        Long userId = accDeliveryAddress.getUserId();

        Preconditions.checkArgument(userId != null, "用户id为不能为空");

        // 1、检查是否设置为默认，如果是默认，则提前将其他默认的记录设为非默认
        if (accDeliveryAddress.getIsDefault()) {
            AccDeliveryAddress temp = new AccDeliveryAddress();
            temp.setIsDefault(false);
            Example example = new Example(AccDeliveryAddress.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("isDefault", true).andEqualTo("userId", userId);
            accDeliveryAddressMapper.updateByExampleSelective(temp, example);
        }

        if (null == accDeliveryAddress.getAddressId()) {
            // 检查是否为新增的第一条记录，如果是新增的第一条记录，则设置为默认
            if (!accDeliveryAddress.getIsDefault()) {
                Example example = new Example(AccDeliveryAddress.class);
                Example.Criteria criteria = example.createCriteria();
                criteria.andEqualTo("userId", userId);
                if (accDeliveryAddressMapper.selectCountByExample(example) == 0) {
                    accDeliveryAddress.setIsDefault(true);
                }
            }

            // 保存新的记录
            Long addressId = super.generateId();
            accDeliveryAddress.setAddressId(addressId);
            accDeliveryAddress.setCreateTime(new Date());
            accDeliveryAddressMapper.insertSelective(accDeliveryAddress);
        } else {
            // 检查是否为只有一条记录，如果是则无论如何都设置为默认
            if (!accDeliveryAddress.getIsDefault()) {
                Example example = new Example(AccDeliveryAddress.class);
                Example.Criteria criteria = example.createCriteria();
                criteria.andEqualTo("userId", userId);
                if (accDeliveryAddressMapper.selectCountByExample(example) == 1) {
                    accDeliveryAddress.setIsDefault(true);
                }
            }

            // 更新记录
            accDeliveryAddress.setUpdateTime(new Date());
            accDeliveryAddressMapper.updateByPrimaryKeySelective(accDeliveryAddress);
        }
    }

    @Override
    public Integer saveUserDeliveryAddress(ShopInfoDto shopInfoDto){
        AccDeliveryAddress accDeliveryAddress = new AccDeliveryAddress();

        accDeliveryAddress.setUserId(shopInfoDto.getUserId());
        accDeliveryAddress.setRegionCountryId(shopInfoDto.getRegionCountryId());
        accDeliveryAddress.setRegionProvinceId(shopInfoDto.getRegionProvinceId());
        accDeliveryAddress.setRegionCityId(shopInfoDto.getRegionCityId());
        accDeliveryAddress.setRegionCountyId(shopInfoDto.getRegionCountyId());
        accDeliveryAddress.setAddress(shopInfoDto.getAddress());
        accDeliveryAddress.setConsignee(shopInfoDto.getShopName());
        accDeliveryAddress.setSex((byte)1);
        accDeliveryAddress.setTelephone(shopInfoDto.getTelephone());
        accDeliveryAddress.setMobile(shopInfoDto.getMobile());
        accDeliveryAddress.setAddressType((byte)0);
        accDeliveryAddress.setIsDefault(true);
        saveDeliveryAddress(accDeliveryAddress);
        return 1;

    }
}
